package com.bk.game.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.bk.game.global.BkGlobal;
import com.bk.game.mc.StartBtn;
import com.bk.game.mc.TopBtn;

/**
 * 描述:游戏主屏幕
 * 
 * @author linqiang(866116)
 * @Since 2012-9-5
 */
public class MainScreen implements Screen, InputProcessor {

	// 舞台
	private Stage stage;
	private TextureAtlas mainTa;
	private boolean loaded;

	private InputMultiplexer multiplexer;

	@Override
	public void dispose() {
		Gdx.input.setInputProcessor(null);
		mainTa.dispose();
		stage.dispose();
	}

	@Override
	public void hide() {

	}

	@Override
	public void pause() {

	}

	@Override
	public void render(float arg0) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
	}

	@Override
	public void resize(int arg0, int arg1) {

	}

	@Override
	public void resume() {

	}

	@Override
	public void show() {
		Gdx.input.setCatchBackKey(true);
		if (!loaded) {
			BkGlobal.btnSound = BkGlobal.manager.get("btn.mp3", Sound.class);
			BkGlobal.buzzerSound = BkGlobal.manager.get("buzzer.mp3", Sound.class);
			stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
			mainTa = BkGlobal.manager.get("main.pack", TextureAtlas.class);
			TextureRegion region = mainTa.findRegion("bg_top");
			Image bg = new Image(region);
			float scalex = (float) Gdx.graphics.getWidth() / region.getRegionWidth();
			float scaley = (float) Gdx.graphics.getHeight() / region.getRegionHeight();
			bg.scaleX = scalex;
			bg.scaleY = scaley;
			stage.addActor(bg);
			initBtn();
			BkGlobal.mainScreen = this;
			loaded = true;

			multiplexer = new InputMultiplexer();
			multiplexer.addProcessor(this);
			multiplexer.addProcessor(stage);
		}

		BkGlobal.score = 0;
		BkGlobal.flyActors.clear();
		BkGlobal.actors.clear();
		Gdx.input.setInputProcessor(multiplexer);
	}

	/**
	 * 描述:初始化按钮
	 * 
	 * @author linqiang(866116)
	 * @Since 2012-9-7
	 */
	private void initBtn() {
		TextureRegion region = mainTa.findRegion("btn_start");
		StartBtn startBtn = new StartBtn(region, 205, 10);
		stage.addActor(startBtn);

		region = mainTa.findRegion("btn_top1");
		float left = 13;
		TopBtn btn1 = new TopBtn("btn_top1", region, left, 10, 88, 90);
		stage.addActor(btn1);

		region = mainTa.findRegion("btn_top2");
		left += 88 + 14;
		TopBtn btn2 = new TopBtn("btn_top2", region, left, 10, 88, 90);
		stage.addActor(btn2);

		region = mainTa.findRegion("btn_top3");
		left += 88 + 232;
		TopBtn btn3 = new TopBtn("btn_top3", region, left, 10, 88, 90);
		stage.addActor(btn3);

		region = mainTa.findRegion("btn_top4");
		left += 88 + 14;
		TopBtn btn4 = new TopBtn("btn_top4", region, left, 10, 88, 90);
		stage.addActor(btn4);
	}

	@Override
	public boolean keyDown(int arg0) {
		return false;
	}

	@Override
	public boolean keyTyped(char arg0) {
		return false;
	}

	@Override
	public boolean keyUp(int arg0) {
		if (arg0 == Input.Keys.BACK) {
			Gdx.app.exit();
		}
		return false;
	}

	@Override
	public boolean scrolled(int arg0) {
		return false;
	}

	@Override
	public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

	@Override
	public boolean touchDragged(int arg0, int arg1, int arg2) {
		return false;
	}

	@Override
	public boolean touchMoved(int arg0, int arg1) {
		return false;
	}

	@Override
	public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
		return false;
	}

}
